package com.hyfrogx.modules.manage.controller.course;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.param.JSONResult;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.entity.VodParam;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.controller.BaseController;
import com.hyfrogx.modules.manage.entity.course.CourseManageEntity;
import com.hyfrogx.modules.manage.entity.course.CourseManageSectionEntity;
import com.hyfrogx.modules.manage.form.course.AddCourseManageSectionForm;
import com.hyfrogx.modules.manage.service.course.CourseManageSectionService;
import com.hyfrogx.modules.manage.service.course.CourseManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;
/**
 * @author 罗春杰-技术部
 */
@RestController
@RequestMapping("courseManageSection")
@Api(value = "CourseManageSectionController",tags = {"后台-课程章节控制系统"})
public class CourseManageSectionController extends BaseController {

    @Autowired
    private CourseManageSectionService sectionService;

    @Autowired
    private CourseManageService manageService;
    /**
     * @param isTranscode/是否转码
     * @param isScreenshot/是否截图
     *            获取签名接口
     */
    @RequestMapping(value = "getSign", method = RequestMethod.POST)
    public @ResponseBody JSONResult getSign(@RequestParam(defaultValue = "0") Integer isTranscode,
                                            @RequestParam(defaultValue = "0") Integer isScreenshot) {

        try {
            Object code = sectionService.getSign(isTranscode, isScreenshot);
            return ajaxSuccess(code);
        } catch (ServiceException e) {
            return ajaxFail(e.getMessage());
        }
    }

    /**
     * 测试新api获取视频信息/
     *
     * @param vodParam
     * @return
     */
    @RequestMapping("getInfoByFileId")
    public @ResponseBody JSONResult getInfoByFileId(VodParam vodParam) {

        if (vodParam.getFileId() == null) {
            return ajaxFail("请选择文件Id");
        }
        try {
            // 开启线程休眠
            Thread.sleep(30000);
            // 新增成功后 关闭线程
            Thread.interrupted();
            // System.out.println("休眠结束");
            Object object = sectionService.getInfoByFileId(vodParam);
            return ajaxSuccess(object);
        } catch (Exception e) {
            Thread.interrupted();
            return ajaxFail(e.getMessage());
        }
    }

    /**
     * 上传COS
     * @param file
     * @return
     */
    @ApiOperation("COS上传")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        // 文件路径
        String url = "";
        // 腾讯云
        url = UploadUtils.uploadWebFile(file, "/course");
        return R.ok().put("filename",url);
    }

    /**
     * 新增章节
     * @param form
     * @return
     */
    @RequestMapping(value ="addSection", method = RequestMethod.POST)
    @ApiOperation("新增章节")
    public R addSection(@RequestBody @Validated AddCourseManageSectionForm form){
        try {
            CourseManageSectionEntity section = new CourseManageSectionEntity();
            BeanUtils.copyProperties(form,section);
            if(section.getSort()== null){
                return R.error("章节排序未填");
            }

            CourseManageEntity courseManage = manageService.getById(section.getProgramid());
            //判断是否收费
            if(courseManage.getIstoll()==1){
                section.setPriceType(1);
            }else{
                section.setPriceType(2);
            }

            section.setCreateDate(new Date());
            sectionService.save(section);
            return R.ok("创建成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
    /**
     * 查看章节
     * @param map
     * @return
     */
    @RequestMapping(value="getAllSection",method=RequestMethod.POST)
    @ApiOperation("查看章节")
    public R getAllSection(@RequestBody Map map){
        try {
            Page page = sectionService.selectSectionPage(new QueryExt(map));
            return R.ok().put("data",page);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除章节
     */
    @ApiOperation("删除章节")
    @RequestMapping(value = "deleteSection",method=RequestMethod.POST)
    public R deleteSection(Integer id){
        try {
            sectionService.removeById(id);
            return R.ok("删除成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 上下架
     */
    @ApiOperation("上下架")
    @RequestMapping(value = "updateIsputaway",method=RequestMethod.POST)
    public R updateIsputaway(Integer id){
        try {
            CourseManageSectionEntity sectionEntity = sectionService.getById(id);
            if(sectionEntity.getIsputaway().equals(FrogConstant.CourseStatus.UPPER)){
                sectionEntity.setIsputaway(FrogConstant.CourseStatus.LOWER);
            }else {
                sectionEntity.setIsputaway(FrogConstant.CourseStatus.UPPER);
            }
            sectionEntity.setEditDate(new Date());
            sectionService.updateById(sectionEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error("未知错误");
        }
    }

    /**
     *  编辑
     */
    @ApiOperation("编辑")
    @RequestMapping(value = "updateCourseManageSection",method=RequestMethod.POST)
    public R updateCourseManageSection(@RequestBody @Validated AddCourseManageSectionForm form){
        try {
            CourseManageSectionEntity section = new CourseManageSectionEntity();
            BeanUtils.copyProperties(form,section);
            boolean rows = sectionService.updateById(section);
            if(rows==true){
                section.setEditDate(new Date());
                return R.ok("编辑成功");
            }else{
                return R.error("编辑失败");
            }

        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

}
